//服务端：
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <zmq.h>

using namespace std;

int main ()
{
    cout << "pub" << endl;
    void *context = zmq_ctx_new ();
    void *publisher = zmq_socket (context, ZMQ_PUB);
    int rc = zmq_bind (publisher, "tcp://*:5558");
    assert (rc == 0);

    //  Initialize random number generator
    srand(time(0));
    while (1) {
        int zipcode, temperature, relhumidity;
        zipcode     = rand() % 100000;
        temperature = rand() % 215 - 80;
        relhumidity = rand() % 50 + 10;

        ostringstream os;
        os << setw(5) << setfill('0')<< zipcode <<" "
           << temperature <<" "<< relhumidity << "\n";
        
        string str1 = "test1 11111111";
        string str2 = "test2 11111111";
        string str3 = "test3 11111111";
        string str4 = "test4 11111111";
        string str5 = "test5 11111111";

        cout << "os : " << os.str() << endl;
        zmq_send(publisher, os.str().c_str(), strlen(os.str().c_str()), 0);

        cout << "str1 : " << str1 << endl;
        zmq_send(publisher, str1.c_str(), strlen(str1.c_str()), 0);

        cout << "str2 : " << str2 << endl;
        zmq_send(publisher, str2.c_str(), strlen(str2.c_str()), 0);

        cout << "str3 : " << str3 << endl;
        zmq_send(publisher, str3.c_str(), strlen(str3.c_str()), 0);

        cout << "str4 : " << str4 << endl;
        zmq_send(publisher, str4.c_str(), strlen(str4.c_str()), 0);

        cout << "str5 : " << str5 << endl;
        zmq_send(publisher, str5.c_str(), strlen(str5.c_str()), 0);

        sleep(1);
    }
    zmq_close (publisher);
    zmq_ctx_destroy (context);
    return 0;
}